package com.cn.unarmed.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.cn.unarmed.activity.R;
import com.cn.unarmed.bean.GridDataModle;

import java.util.List;

/**
 * Created by Administrator on 2017/7/6.
 * 自定义recyclerView适配器
 */
public class MyGridRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{

    private List<GridDataModle> list;
    private Context context;
    private ItemClickLisener mItemClickListener;

    public MyGridRecyclerViewAdapter(Context context, List<GridDataModle> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == 1) {
            view = LayoutInflater.from(context).inflate(R.layout.grid_item_1, parent, false);
            return new GridXmlHolder1(view);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.grid_item_2, parent, false);
            return new GridXmlHolder2(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof  GridXmlHolder1){
            ((GridXmlHolder1) holder).setTextName(String.valueOf(list.get(position).getName()));
            ((GridXmlHolder1) holder).setToggleCheck(false);
            ((GridXmlHolder1) holder).btn_toggle.setTag(position);
            ((GridXmlHolder1) holder).grid_item_lay_1.setTag(position);
            ((GridXmlHolder1) holder).btn_toggle.setOnClickListener(this);
            ((GridXmlHolder1) holder).grid_item_lay_1.setOnClickListener(this);

        } else {
            ((GridXmlHolder2) holder).setTextName(String.valueOf(list.get(position).getName()));
            ((GridXmlHolder2) holder).setGrid_check(false);
            ((GridXmlHolder2) holder).grid_check.setTag(position);
            ((GridXmlHolder2) holder).grid_item_lay_2.setTag(position);
            ((GridXmlHolder2) holder).grid_check.setOnClickListener(this);
            ((GridXmlHolder2) holder).grid_item_lay_2.setOnClickListener(this);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType_lay();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_toggle:
                mItemClickListener.onCheck(((ToggleButton)v).isChecked(),(int)v.getTag());
                break;
            case R.id.grid_check:
                mItemClickListener.onCheck(((CheckBox)v).isChecked(),(int)v.getTag());
                break;
            case R.id.grid_item_lay_1:
                mItemClickListener.onItemClick((int)v.getTag());
                break;
            case R.id.grid_item_lay_2:
                mItemClickListener.onItemClick((int)v.getTag());
                break;
        }
    }

    public interface ItemClickLisener{
        void onItemClick(int position);
        void onCheck(boolean ischeck,int position);
    }

    public void setItemClickListener(ItemClickLisener mItemClickListener){
        this.mItemClickListener = mItemClickListener;
    }

    private class GridXmlHolder1 extends RecyclerView.ViewHolder{

        LinearLayout grid_item_lay_1;
        TextView text_name;
        ToggleButton btn_toggle;

        GridXmlHolder1(View itemView) {
            super(itemView);
            grid_item_lay_1 = (LinearLayout) itemView.findViewById(R.id.grid_item_lay_1);
            text_name = (TextView) itemView.findViewById(R.id.text_name);
            btn_toggle = (ToggleButton) itemView.findViewById(R.id.btn_toggle);
        }

        void setTextName(String x) {
            text_name.setText(x);
        }

        void setToggleCheck(boolean check) {
            btn_toggle.setChecked(check);
        }

    }

    private class GridXmlHolder2 extends RecyclerView.ViewHolder {

        LinearLayout grid_item_lay_2;
        TextView text_grid_name;
        CheckBox grid_check;

        GridXmlHolder2(View itemView) {
            super(itemView);
            grid_item_lay_2 = (LinearLayout) itemView.findViewById(R.id.grid_item_lay_2);
            text_grid_name = (TextView) itemView.findViewById(R.id.text_grid_name);
            grid_check = (CheckBox) itemView.findViewById(R.id.grid_check);
        }

        void setTextName(String x) {
            text_grid_name.setText(x);
        }

        void setGrid_check(boolean check) {
            grid_check.setChecked(check);
        }
    }
}
